#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2022-2-21 16:02
# @Author : huabao.tan
# @File : baidu_picture.py
# @Software: PyCharm

import requests
import tkinter as tk
from tkinter import filedialog

def save_file():
    root = tk.Tk()
    root.withdraw()
    Fpath = filedialog.asksaveasfilename()
    return Fpath

def get_picture():
    work = input('输入想要下载的图片：')
    page = input('请输入要获取多少页(百度图片下滑时默认一页显示30张)：')
    c = input('选择图片的width（建议1920或者2560，默认直接回车:）')
    b = input('选择图片的height（建议1080或者1440，默认直接回车:）')
    page = int(page) + 1
    header = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
    }
    a = 1
    n = 0
    pn = 1
    # pn是从第几张图片获取 百度图片下滑时默认一次性显示30张
    file_path = save_file()
    for m in range(1, page):
        url = 'https://image.baidu.com/search/acjson?'
        param = {
            'tn': 'resultjson_com',
            'logid': '8846269338939606587',
            'ipn': 'rj',
            'ct': '201326592',
            'is': '',
            'fp': 'result',
            'queryWord': '%s'%work,
            'cl': '2',
            'lm': '-1',
            'ie': 'utf-8',
            'oe': 'utf-8',
            'adpicid': '',
            'st': '-1',
            'z': '',
            'ic': '',
            'hd': '',
            'latest': '',
            'copyright': '',
            'word': '%s'%work,
            's': '',
            'se': '',
            'tab': '',
            'width': '%s'%c,
            'height': '%s'%b,
            'face': '0',
            'istype': '2',
            'qc': '',
            'nc': '1',
            'fr': '',
            'expermode': '',
            'force': '',
            'cg': 'girl',
            'pn': pn,  # 从第几张图片开始
            'rn': '30',
            'gsm': '1e',
        }
        page_text = requests.get(url=url, headers=header, params=param)
        page_text.encoding = 'utf-8'
        page_text = page_text.json(strict=False)
        info_list = page_text['data']
        del info_list[-1]
        img_path_list = []
        for i in info_list:
            img_path_list.append(i['thumbURL'])

        for img_path in img_path_list:
            img_data = requests.get(url=img_path, headers=header).content
            img_path1 = file_path + work +str(a) + '.jpg'
            with open(img_path1, 'wb') as fp:
                print("正在下载中","文件路径为%s"%img_path1)
                fp.write(img_data)
            n = n + 1
            a += 1
        pn += 29

def stat():
    while True:
        get_picture()
        print('下载完成')
        res = input('是否继续y/n')
        if res == 'y':
            continue
        else:
            break
if __name__ == '__main__':
   stat()

   #pyinstaller -F pytest.py 打包命令

